#!/usr/bin/perl -w
use strict;
use warnings;
use DBI;
use LWP;
use HTTP::Request::Common;
use URI::Escape;
use Digest::MD5 qw(md5_hex);
require('function.pl');

my %city_map = load_city_digit_map('58');
my $db_host = '127.0.0.1';
my $db = 'spam';
my $db_user = 'zjxia';
my $db_password = 'bblovebb';
my $db_conn = DBI->connect("DBI:mysql:database=$db;host=$db_host", $db_user, $db_password);
#$db_conn->do('set names gb2312');
my @accounts = load_accounts('58');
my @proxies = load_proxies();
my $sql = 'select * from post';
my $request = $db_conn->prepare($sql);
$request->execute;
while (my @row = $request->fetchrow_array) {
	if ($row[9] eq 'jiajiao') {
		process_jiajiao(@row);
	}
}
$db_conn->disconnect;

sub load_accounts {
	my @accounts;
	my $sql = "select user_name, password from account where domain = '$_[0]' and valid = 1";
	my $request = $db_conn->prepare($sql);
	$request->execute;
	while (my @row = $request->fetchrow_array) {
		push @accounts, \@row;
	}
	return @accounts;
}

sub get_md5_rand {
	my $md5 = substr(md5_hex(rand()), 0, 5);
	return $md5;
	$md5 =~ s/\d/ /g;
	return $md5;
}
sub process_jiajiao {
	my ($post_id, $source, $title, $subject, $type, $business_hours, $address, $contact, $phone_num, $template_type, $content) = @_;
	my %post = (
	'Content' => uri_escape($content),
	'ObjectType' => uri_escape($subject),
	'cateID' => 14286,
	'clienttype' => 3,
	'dizhi' => uri_escape($address),
	'fuwuleixing' => uri_escape($type),
	'goblianxiren' => uri_escape($contact),
	'laiyuan' => uri_escape($source),
#	'localid' => 0,
	'phone' => $phone_num,
#	$pwd => '',
	'title' => uri_escape($title),
#	'uname' => '',
	'userID' => 30,
	'yingyeshijian' => uri_escape($business_hours),
	'title_message' => uri_escape('请填写标题#6-25个字,请勿填写电话和特殊符号#6-25个字,请勿填写电话和特殊符号'),
	'Content_message' => uri_escape('请填写描述内容#描述不足10个字#内容应在10~2000个字之间,不能填写电话、QQ等联系方式'),
	'dizhi_message' => uri_escape('请填写地址#地址里不能填写电话#地址里不能填写特殊字符'),
	'dizhi_regex' => uri_escape('^(.){2,50}$#^([^\\s]\\s{0,3}(?!([0-9０１２３４５６７８９零一二三四五六七八九壹贰叁肆伍陆柒捌玖]{6})))+$#^[\\w\\[\\]\\(\\)\\{\\}\\.\\s,\\-\\*,。（）【】“”—‘’\\u4e00-\\u9fa5]*$'),
	'phone_message' => uri_escape('	^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\\d{8}$'),
	'title_regex' => uri_escape('^[\\s\\S]*$#^([^`~!$%^*()+=|{}‘；：”“’。，、？/?~！%*（）——+|{}【】\':;\',\\[\\].]){6,25}$#^([^\\s]\\s{0,6}(?!([0-9０１２３４５６７８９零一二三四五六七八九壹贰叁肆伍陆柒捌玖]{6}|\\s)))+$'),
	'yingyeshijian_message' => uri_escape('请按格式输入，如：08:00-24:00'),
	'yingyeshijian_regex' => uri_escape('\\d{2,2}：\\d{2,2}-\\d{2,2}：\\d{2,2}$|\\d{2,2}:\\d{2,2}-\\d{2,2}:\\d{2,2}$|08:00-24:00'),
	'Content_message' => uri_escape('请填写描述内容#描述不足10个字#内容应在10~2000个字之间,不能填写电话、QQ等联系方式'),
	'Content_regex' => uri_escape('^[\\s\\S]*$#^[\\s\\S]{10,}$#^([\\s\\S]){10,2000}$'),
	'' => uri_escape(''),
	'' => uri_escape(''),
	'' => uri_escape(''),
	'' => uri_escape('')
	);
#	print "$title\t";
#	print uri_escape($title)."\n";
#	print "$content\t".uri_escape($content)."\n";
#	exit;
	my $agent = LWP::UserAgent->new();
	$agent->timeout(5);
	my $account_index = -1;
	my $proxy_index = 0;
	while (my ($city, $city_id) = each %city_map) {
		while (1) {
			if (++$account_index == scalar @accounts) {
				@accounts = load_accounts('58');
				$account_index = 0;
			}
	my $args = 'cateID=14286&userID=30&clienttype=3&laiyuan='.uri_escape($source).'&title_regex=%5E%5B%5Cs%5CS%5D*%24%23%5E%28%5B%5E%60%7E%21%24%25%5E*%28%29%2B%3D%7C%7B%7D%E2%80%98%EF%BC%9B%EF%BC%9A%E2%80%9D%E2%80%9C%E2%80%99%E3%80%82%EF%BC%8C%E3%80%81%EF%BC%9F%2F%3F%7E%EF%BC%81%25*%EF%BC%88%EF%BC%89%E2%80%94%E2%80%94%2B%7C%7B%7D%E3%80%90%E3%80%91%27%3A%3B%27%2C%5C%5C%5B%5C%5C%5D.%5D%29%7B6%2C25%7D%24%23%5E%28%5B%5E%5Cs%5D%5Cs%7B0%2C6%7D%28%3F%21%28%5B0-9%EF%BC%90%EF%BC%91%EF%BC%92%EF%BC%93%EF%BC%94%EF%BC%95%EF%BC%96%EF%BC%97%EF%BC%98%EF%BC%99%E9%9B%B6%E4%B8%80%E4%BA%8C%E4%B8%89%E5%9B%9B%E4%BA%94%E5%85%AD%E4%B8%83%E5%85%AB%E4%B9%9D%E5%A3%B9%E8%B4%B0%E5%8F%81%E8%82%86%E4%BC%8D%E9%99%86%E6%9F%92%E6%8D%8C%E7%8E%96%5D%7B6%7D%7C%5Cs%29%29%29%2B%24&title_message=%E8%AF%B7%E5%A1%AB%E5%86%99%E6%A0%87%E9%A2%98%236-25%E4%B8%AA%E5%AD%97%2C%E8%AF%B7%E5%8B%BF%E5%A1%AB%E5%86%99%E7%94%B5%E8%AF%9D%E5%92%8C%E7%89%B9%E6%AE%8A%E7%AC%A6%E5%8F%B7%236-25%E4%B8%AA%E5%AD%97%2C%E8%AF%B7%E5%8B%BF%E5%A1%AB%E5%86%99%E7%94%B5%E8%AF%9D%E5%92%8C%E7%89%B9%E6%AE%8A%E7%AC%A6%E5%8F%B7&title='.uri_escape($title.' '.get_md5_rand()).'&ObjectType='.uri_escape($subject).'&fuwuleixing='.uri_escape($type).'&yingyeshijian_regex=%5Cd%7B2%2C2%7D%EF%BC%9A%5Cd%7B2%2C2%7D-%5Cd%7B2%2C2%7D%EF%BC%9A%5Cd%7B2%2C2%7D%24%7C%5Cd%7B2%2C2%7D%3A%5Cd%7B2%2C2%7D-%5Cd%7B2%2C2%7D%3A%5Cd%7B2%2C2%7D%24%7C08%3A00-24%3A00&yingyeshijian_message=%E8%AF%B7%E6%8C%89%E6%A0%BC%E5%BC%8F%E8%BE%93%E5%85%A5%EF%BC%8C%E5%A6%82%EF%BC%9A08%3A00-24%3A00&yingyeshijian='.uri_escape($business_hours).'&dizhi_regex=%5E%28.%29%7B2%2C50%7D%24%23%5E%28%5B%5E%5Cs%5D%5Cs%7B0%2C3%7D%28%3F%21%28%5B0-9%EF%BC%90%EF%BC%91%EF%BC%92%EF%BC%93%EF%BC%94%EF%BC%95%EF%BC%96%EF%BC%97%EF%BC%98%EF%BC%99%E9%9B%B6%E4%B8%80%E4%BA%8C%E4%B8%89%E5%9B%9B%E4%BA%94%E5%85%AD%E4%B8%83%E5%85%AB%E4%B9%9D%E5%A3%B9%E8%B4%B0%E5%8F%81%E8%82%86%E4%BC%8D%E9%99%86%E6%9F%92%E6%8D%8C%E7%8E%96%5D%7B6%7D%29%29%29%2B%24%23%5E%5B%5Cw%5C%5B%5C%5D%5C%28%5C%29%5C%7B%5C%7D%5C.%5Cs%2C%5C-%5C*%2C%E3%80%82%EF%BC%88%EF%BC%89%E3%80%90%E3%80%91%E2%80%9C%E2%80%9D%E2%80%94%E2%80%98%E2%80%99%5Cu4e00-%5Cu9fa5%5D*%24&dizhi_message=%E8%AF%B7%E5%A1%AB%E5%86%99%E5%9C%B0%E5%9D%80%23%E5%9C%B0%E5%9D%80%E9%87%8C%E4%B8%8D%E8%83%BD%E5%A1%AB%E5%86%99%E7%94%B5%E8%AF%9D%23%E5%9C%B0%E5%9D%80%E9%87%8C%E4%B8%8D%E8%83%BD%E5%A1%AB%E5%86%99%E7%89%B9%E6%AE%8A%E5%AD%97%E7%AC%A6&dizhi='.uri_escape($address).'&Content_regex=%5E%5B%5Cs%5CS%5D*%24%23%5E%5B%5Cs%5CS%5D%7B10%2C%7D%24%23%5E%28%5B%5Cs%5CS%5D%29%7B10%2C2000%7D%24&Content_message=%E8%AF%B7%E5%A1%AB%E5%86%99%E6%8F%8F%E8%BF%B0%E5%86%85%E5%AE%B9%23%E6%8F%8F%E8%BF%B0%E4%B8%8D%E8%B6%B310%E4%B8%AA%E5%AD%97%23%E5%86%85%E5%AE%B9%E5%BA%94%E5%9C%A810%7E2000%E4%B8%AA%E5%AD%97%E4%B9%8B%E9%97%B4%2C%E4%B8%8D%E8%83%BD%E5%A1%AB%E5%86%99%E7%94%B5%E8%AF%9D%E3%80%81QQ%E7%AD%89%E8%81%94%E7%B3%BB%E6%96%B9%E5%BC%8F&Content='.uri_escape($content."\n".get_md5_rand()).'&goblianxiren='.uri_escape($contact).'&phone_regex=%5E%2813%5B0-9%5D%7C15%5B0%7C3%7C6%7C7%7C8%7C9%5D%7C18%5B8%7C9%5D%29%5Cd%7B8%7D%24&phone_message=%E6%89%8B%E6%9C%BA%E5%8F%B7%E7%A0%81%E6%A0%BC%E5%BC%8F%E4%B8%8D%E6%AD%A3%E7%A1%AE&phone='.$phone_num;
			$post{'localid'} = $city_id;
			$post{'uname'} = $accounts[$account_index]->[0];
			$post{'pwd'} = $accounts[$account_index]->[1];
			my $req = new HTTP::Request POST => 'http://user.wap.58.com/postaction.do';
			$req->content_type('application/x-www-form-urlencoded');
			$req->content($args.'&uname='.$accounts[$account_index]->[0].'&pwd='.$accounts[$account_index]->[1].'&localid='.$city_id);
			$agent->proxy('http', 'http://'.$proxies[++$proxy_index % @proxies]);
			my $response = $agent->request($req);
#			my $response = $agent->post('http://user.wap.58.com/postaction.do', \%post);
#			my $content = $agent->request($req);
			my $content = $response->content;
			print $content."\n";
			my $result = 0;
			if ($content =~ /<a href='(.+?)'>这里查看发布的效果<\/a>/) {
				if (index(`curl -s '$1'`, $subject) > 0) {
					$result = 1;
					print "post ".$accounts[$account_index]->[0]." url $1\n";
					$sql = "select count(*) from record where post_id = $post_id and city = '$city'";
					if (execute_scalar($sql, $db_conn) > 0) {
						$sql = "update record set url = '$1' where post_id = $post_id and city = '$city'";

					}
					else {
						$sql = "insert into record(post_id, city, url) values($post_id, '$city', '$1')";
					}
					$db_conn->do($sql);
				}
			}
			if ($result) {
				last;
			}
			else {
				print STDERR "$city post id $post_id\n";
			}
		}
	}
}

